﻿using Convertor.Interface;
using System;
using System.IO;
using System.Windows;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.SelectDirectory
{
    public class Plugin : PluginBase
    {
        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.SelectDirectory;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.SelectDirectory;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_File") + " - " + UILanguage.FindResource("Plugin_SelectDirectory_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_SelectDirectory_Desc"); }
        }

        

        

        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }

            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            string path = content.OutputContent.ToString().Trim();
            if (Directory.Exists(path))
            {
                config.SaveFilePath = path;
            }

            if (string.IsNullOrEmpty(config.SaveFilePath))
            {
                if (Loader.ShowDialog() == true)
                {
                    config = ((IFormGetConfigEvent)Loader.Loader).GetConfig() as ConfigContent;
                    ((IConvert)this).Output(content);
                }
            }

            if (Log != null) { Log(this, UILanguage.FindResource("Plugin_SelectDirectory_Result"), LogTypes.Normal | LogTypes.HidePluginName); }
            return new ConvertParamters(content.LastInputContent, config.SaveFilePath);
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

    }
}
